package com.frxuexi.park2210.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.LiveDataBus
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.park2210.R
import com.frxuexi.park2210.databinding.FragmentHomeBinding
import com.frxuexi.park2210.user.UserViewModel
import com.tencent.smtt.sdk.WebView
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus


@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding,UserViewModel>() {
//    lateinit var binding:FragmentHomeBinding
    lateinit var wv:WebView
    lateinit var listener:WebViewFun

    /*override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
//        binding= FragmentHomeBinding.inflate(layoutInflater, container, false)
        wv=binding.wv

        listener=WebViewFun()
        wv.loadUrl(SPUtils.getInstance().getString("home"))
        wv.settings.javaScriptEnabled=true
        wv.addJavascriptInterface(listener,"androidinfo")


        return binding.root
    }*/

    override fun initData() {

        LiveDataBus.with("message").observe(requireActivity(),object :Observer<String>{
            override fun onChanged(value: String) {
                if(!value.isNullOrEmpty()){
                    ToastUtils.showLong(value)
                }
            }

        })
    }

    override fun initView() {
        wv=binding.wv

        listener=WebViewFun()
        wv.loadUrl(SPUtils.getInstance().getString("home"))
        wv.settings.javaScriptEnabled=true
        wv.addJavascriptInterface(listener,"androidinfo")
    }

    inner class WebViewFun{

        @JavascriptInterface
        fun androidsign(id: String) {
            //打开签到activity
            Log.i("===","签到点击了")
            start("/app/SignActivity")
        }
        @JavascriptInterface
        fun androidaddculture(id: String) {
            //打开新闻activity
        }
        @JavascriptInterface
        fun androidparking(id: String?) {
            //车位申请
            start("/app/ParkActivity")
        }
        @JavascriptInterface
        fun androidapply(id: String?) {
            //我的申请
        }
        @JavascriptInterface
        fun androidvisit(id: String?) {
            //来访预约
            start("/app/VistorActivity")
        }
        @JavascriptInterface
        fun androidpatrol(id: String?) {
            //巡更管理
//            EventBus.getDefault().post("消息来了")
        }
        @JavascriptInterface
        fun androidnews(id: String?) {
        }
        @JavascriptInterface
        fun androidnoticelist(id: String?) {
        }
        @JavascriptInterface
        fun androidrepair(id: String?) {
            //维修管理
            start("/app/RepairActivity")
        }
        @JavascriptInterface
        fun androidaddrepair(id: String?) {
            //添加维修
            start("/app/RepairAddActivity")
        }
        @JavascriptInterface
        fun androidaddnotice(id: String?) {
            //公告管理
            start("/app/NoticeActivity")
        }
        @JavascriptInterface
        fun androidculture(id: String?) {
            //文化管理
            start("/app/NewsActivity")
        }
        @JavascriptInterface
        fun androidnotice(id: String?) {
            //公告管理
        }
        @JavascriptInterface
        fun androidattendance(id: String?) {
            //考勤管理
            start("/app/SignListActivity")
        }
        @JavascriptInterface
        fun androidproperty(id: String?) {
            //物业审核
        }
        @JavascriptInterface
        fun androidcheckculture(id: String?) {
            //文化审核
        }
        @JavascriptInterface
        fun androidpeople(id: String?) {
            //人资管理
        }
    }

    fun start(url:String){
        ARouter.getInstance().build(url).navigation(requireActivity(),object :NavigationCallback{
            override fun onFound(postcard: Postcard?) {

            }

            override fun onLost(postcard: Postcard?) {
            }

            override fun onArrival(postcard: Postcard?) {
            }

            override fun onInterrupt(postcard: Postcard?) {
                ToastUtils.showLong("登录拦截")
            }

        })
    }

    override fun onDestroy() {
        super.onDestroy()
        wv.clearHistory()
        wv.destroy()
    }
}